package com.scm.biz.service._impl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scm.biz.bean.OrderTaskFlow;
import com.scm.biz.bean.SysRole;
import com.scm.biz.dao.DesignOrderDao;
import com.scm.biz.dao.OrderTaskFlowDao;
import com.scm.biz.enums.OrderTypeEnum;
import com.scm.biz.service.OrderTaskFlowService;
import com.scm.biz.service.SysRoleService;
@Service
public class OrderTaskFlowServiceImpl implements OrderTaskFlowService {

	@Autowired
	OrderTaskFlowDao orderTaskFlowDao;
	@Autowired
	DesignOrderDao designOrderDao;
	@Autowired
	SysRoleService sysRoleService;
	/*
	 * 指派给具体权限的人
	 * @see com.scm.biz.service.OrderTaskFlowService#addPerformer(com.scm.biz.enums.OrderTypeEnum, java.lang.String, java.lang.Long[])
	 */
	@Override
	public void addPerformer(OrderTypeEnum orderTypeEnum,String action,String roleCodes,Long... businessIds)
	 {
		for(String roleCode:roleCodes.split(",")){
			SysRole sysRole = sysRoleService.selectByCode(roleCode);
			OrderTaskFlow orderTaskFlow = null;
			for(Long businessId:businessIds){
				//如果存在则删除
				orderTaskFlowDao.deleteByAction(businessId, action,0);
				//更新上一级别为已完成
				orderTaskFlowDao.updateByBusinessId(businessId,orderTypeEnum.toString());
				orderTaskFlow = new OrderTaskFlow();
				orderTaskFlow.setAction(action);
				orderTaskFlow.setAssigner(Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString()));
				orderTaskFlow.setBusinessId(Long.valueOf(businessId));
				orderTaskFlow.setCreateAt(new Date());
				orderTaskFlow.setOrderType(orderTypeEnum.toString());
				orderTaskFlow.setPerformer(sysRole.getId());
				orderTaskFlow.setAssignerType(1);//角色
				orderTaskFlow.setStatus(0);//设计中
				orderTaskFlowDao.insert(orderTaskFlow);
			}
		}
	}
	@Override
	public void update(OrderTaskFlow orderTaskFlow) {
		orderTaskFlowDao.updateByPrimaryKey(orderTaskFlow);
	}


//	@Override
//	public List<OrderTaskFlow> selectByUnionCode(Long businessId,String unionCode,Integer status) {
//
//		return orderTaskFlowDao.selectByUnionCode(businessId, unionCode,status);
//	}

	@Override
	public void insert(OrderTaskFlow orderTaskFlow) {
		orderTaskFlowDao.insert(orderTaskFlow);
	}
	/**
	 * 指派给某个人
	 */
	@Override
	public void addPerformer(OrderTypeEnum orderTypeEnum, String action,
			Integer assignUser, Long... businessIds) {
		OrderTaskFlow orderTaskFlow = null;
		for(Long businessId:businessIds){
			//如果存在则删除
			orderTaskFlowDao.deleteByAction(businessId, action,0);
			orderTaskFlowDao.updateByBusinessId(businessId,orderTypeEnum.toString());
	
			orderTaskFlow = new OrderTaskFlow();
			orderTaskFlow.setAction(action);
			orderTaskFlow.setAssigner(Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString()));
			orderTaskFlow.setBusinessId(Long.valueOf(businessId));
			orderTaskFlow.setCreateAt(new Date());
			orderTaskFlow.setOrderType(orderTypeEnum.toString());
			orderTaskFlow.setPerformer(assignUser);
			orderTaskFlow.setStatus(0);//设计中
			orderTaskFlow.setAssignerType(0);//用户
			orderTaskFlowDao.insert(orderTaskFlow);
		}
		
	}
//	@Override
//	public void addUnionPerformer(OrderTypeEnum orderTypeEnum,
//			String actions, Long... businessIds) {
//		for(String action:actions.split(",")){
//			List<Integer> performerList = sysRoleService.selectListByResourceCode(action);
//			OrderTaskFlow orderTaskFlow = null;
//			for(Long businessId:businessIds){
//				orderTaskFlowDao.deleteByAction(businessId, action,0);
//				orderTaskFlowDao.updateByBusinessId(businessId,orderTypeEnum.toString());
//				if(performerList !=null){
//					for(Integer performer:performerList){
//						orderTaskFlow = new OrderTaskFlow();
//						orderTaskFlow.setAction(action);
//						orderTaskFlow.setAssigner(Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString()));
//						orderTaskFlow.setBusinessId(Long.valueOf(businessId));
//						orderTaskFlow.setCreateAt(new Date());
//						orderTaskFlow.setOrderType(orderTypeEnum.toString());
//						orderTaskFlow.setPerformer(performer);
//						orderTaskFlow.setStatus(0);//设计中
//						orderTaskFlow.setAssignerType(1);//角色
//						orderTaskFlow.setUnionCode(actions);
//						orderTaskFlowDao.insert(orderTaskFlow);
//					}
//				}
//			}
//		}
//		
//	}



}
